package cn.edu.ldh.toolapp.slice;

import cn.edu.ldh.toolapp.ResourceTable;
import cn.edu.ldh.toolapp.entity.NoteItem;
import cn.edu.ldh.toolapp.entity.NoteItemRes;
import cn.edu.ldh.toolapp.stores.NoteStore;
import cn.edu.ldh.toolapp.util.ToastUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;



public class NoteDetailAbilitySlice extends AbilitySlice {

    Button editBtn;
    Button saveBtn;
    Button delBtn;
    TextField titleTf;
    TextField contentTf;

    private final static HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 1, "NoteDetailAbilitySlice");
    private NoteItem noteItem;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_notedetail);
        this.startInit(intent);
    }

    //获取传入数据和初始化组件
    private void startInit(Intent intent) {
        noteItem = intent.getSerializableParam("noteItem");
        titleTf = (TextField) findComponentById(ResourceTable.Id_note_detail_title);
        contentTf = (TextField) findComponentById(ResourceTable.Id_note_detail_content);
        titleTf.setText(noteItem.getNoteTitle());
        contentTf.setText(noteItem.getNoteContent());
        editBtn = (Button) findComponentById(ResourceTable.Id_btn_detail_edit);
        saveBtn = (Button) findComponentById(ResourceTable.Id_btn_detail_save);
        delBtn = (Button) findComponentById(ResourceTable.Id_btn_detail_del);
        editBtn.setClickedListener(this::editTf);
        saveBtn.setClickedListener(this::saveTf);
        delBtn.setClickedListener(this::delTf);
    }

    //删除日记
    private void delTf(Component component) {
        NoteItemRes noteItemRes = NoteStore.getInstance().deleteNote(noteItem.getNoteId());
        if(noteItemRes.getCode() == 200) {
            ToastUtil.showToast(getContext(), noteItemRes.getMessage());
            present(new MainAbilitySlice(), new Intent());
        } else {
            ToastUtil.showToast(getContext(), noteItemRes.getMessage());
        }
    }

    //保存修改
    private void saveTf(Component component) {
        //TODO 更新方法
        NoteItem newNoteItem = new NoteItem();
        newNoteItem.setNoteId(noteItem.getNoteId());
        newNoteItem.setNoteTitle(titleTf.getText());
        newNoteItem.setNoteContent(contentTf.getText());
        newNoteItem.setNoteDate(noteItem.getNoteDate());
        NoteItemRes noteItemRes = NoteStore.getInstance().updateNote(newNoteItem);
        ToastUtil.showToast(getContext(), noteItemRes.getMessage());
    }

    //改变输入框状态
    private void editTf(Component component) {
        HiLog.error(LABEL, String.valueOf("修改前" + titleTf.isEnabled()));
        //如果两个输入框都是禁用状态，则把状态改为true可编辑
        if(titleTf.isEnabled() || contentTf.isEnabled()) {
            titleTf.clearFocus();
            contentTf.clearFocus();
            titleTf.setEnabled(false);
            contentTf.setEnabled(false);
            HiLog.error(LABEL, String.valueOf("修改后" + titleTf.isEnabled()));
            editBtn.setText("编辑");
        } else if(!titleTf.isEnabled() || !contentTf.isEnabled()) {
            titleTf.setEnabled(true);
            contentTf.setEnabled(true);
            HiLog.error(LABEL, String.valueOf("修改后" + titleTf.isEnabled()));
            editBtn.setText("完成");
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
